package com.tinyengine.it.service.endpoint.impl;

import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.tinyengine.it.mapper.ApiDirectoryMapper;
import com.tinyengine.it.mapper.ApiEndpointsMapper;
import com.tinyengine.it.mapper.ApiGatewayServiceMapper;
import com.tinyengine.it.model.entity.ApiDirectory;
import com.tinyengine.it.model.entity.ApiEndpoints;
import com.tinyengine.it.model.entity.ApiGatewayService;
import com.tinyengine.it.model.vo.ApiDirectoryVo;
import com.tinyengine.it.service.endpoint.ApiDirectoryService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author Administrator
 * @description 针对表【i_api_projects(接口项目信息)】的数据库操作Service实现
 * @createDate 2025-03-03 17:24:42
 */
@Service
public class ApiDirectoryServiceImpl extends ServiceImpl<ApiDirectoryMapper, ApiDirectory>
        implements ApiDirectoryService {

    @Resource
    private ApiDirectoryMapper apiDirectoryMapper;
    @Resource
    private ApiEndpointsMapper apiEndpointsMapper;
    @Resource
    private ApiGatewayServiceMapper apiGatewayServiceMapper;

    @Override
    public List<ApiDirectoryVo> getAllDirectory(Integer appId) {

        List<ApiDirectoryVo> projects = apiDirectoryMapper.getAllDirectoryWithHierarchy(appId);

        LambdaQueryWrapper<ApiGatewayService> apiGatewayServiceLambdaQueryWrapper = Wrappers.lambdaQuery();
        if (ObjUtil.isNotNull(appId)) {
            apiGatewayServiceLambdaQueryWrapper.eq(ApiGatewayService::getAppId, appId);
        }
        List<ApiGatewayService> gatewayServices = apiGatewayServiceMapper.selectList(apiGatewayServiceLambdaQueryWrapper);
        gatewayServices.forEach(gatewayService -> projects.add(new ApiDirectoryVo().setId(gatewayService.getId())
                .setCode(gatewayService.getServiceEn())
                .setParentId(0)
                .setType(1)
                .setName(gatewayService.getServiceName())));

        projects.forEach(e -> e.setChildren(
                apiEndpointsMapper.selectList(Wrappers.<ApiEndpoints>lambdaQuery()
                                .eq(ApiEndpoints::getDirectoryId, e.getId())
                                .select(ApiEndpoints::getId, ApiEndpoints::getDirectoryId, ApiEndpoints::getStatus, ApiEndpoints::getName))
                        .stream().map(endpoint -> {
                            ApiDirectoryVo vo = new ApiDirectoryVo();
                            vo.setType(3);
                            BeanUtils.copyProperties(endpoint, vo);
                            return vo;
                        }).collect(Collectors.toList())));
        return buildTree(projects);
    }

    public static List<ApiDirectoryVo> buildTree(List<ApiDirectoryVo> nodes) {
        Map<Integer, ApiDirectoryVo> nodeMap = new HashMap<>();
        List<ApiDirectoryVo> roots = new ArrayList<>();
        for (ApiDirectoryVo node : nodes) {
            nodeMap.put(node.getId(), node);
        }
        for (ApiDirectoryVo node : nodes) {
            if (node.getParentId() == 0) {
                roots.add(node);
            } else {
                ApiDirectoryVo parent = nodeMap.get(node.getParentId());
                if (parent != null) {
                    parent.addChild(node);
                }
            }
        }
        return roots;
    }
}




